package com.example.demo.service.impl;

import com.example.demo.dataobject.User;
import com.example.demo.dataobject.UserInfo;
import com.example.demo.repository.UserInfoRepository;
import com.example.demo.service.UserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class UserInfoServiceImpl implements UserInfoService {

    @Autowired
    private UserInfoRepository userInfoRepository;

    @Override
    public boolean validateLogin(String telephone, String encryptPassword) {
        UserInfo userInfo = userInfoRepository.findByTelephone(telephone);
        if (userInfo == null) {
            return false;
        }
        if (!StringUtils.equals(encryptPassword, userInfo.getEncryptPassword())) {
            return false;
        }
        return true;
    }

    @Override
    public boolean register(String telephone, String encryptPassword) {
        if (StringUtils.isEmpty(telephone) || StringUtils.isEmpty(encryptPassword)) {
            log.info("用户名或密码为空");
            return false;
        }
        UserInfo result = userInfoRepository.findByTelephone(telephone);
        if (result != null) {
            log.info("用户已被注册");
            return false;
        }
        UserInfo userInfo = new UserInfo(telephone, encryptPassword);
        UserInfo save = userInfoRepository.save(userInfo);
        if (save != null) {
            return true;
        }
        return false;
    }

    @Override
    public boolean modifyPassword(String telephone, String originalPassword, String currentPassword) {
        if (StringUtils.isEmpty(telephone) || StringUtils.isEmpty(originalPassword) || StringUtils.isEmpty(currentPassword)) {
            log.error("手机号、原始密码和修改后的密码中存在空值");
            return false;
        }
        UserInfo userInfo = userInfoRepository.findByTelephone(telephone);
        if (userInfo == null) {
            return false;
        } else if (!userInfo.getEncryptPassword().equals(originalPassword)) {
            return false;
        }

        userInfo.setEncryptPassword(currentPassword);
        UserInfo save = userInfoRepository.save(userInfo);
        if (save != null) {
            return true;
        }
        return false;


    }

    @Override
    public boolean modifyNickname(String telephone,  String nickname) {
        UserInfo userInfo = userInfoRepository.findByTelephone(telephone);
        if (userInfo == null) {
            return false;
        }
        userInfo.setNickname(nickname);
        UserInfo save = userInfoRepository.save(userInfo);
        if (save != null) {
            return true;
        }
        return false;
    }

    @Override
    public boolean modifyTelephone(String originalTelephone, String password, String currentTelephone) {
        UserInfo userInfo = userInfoRepository.findByTelephone(originalTelephone);
        if (userInfo == null) {
            return false;
        } else if (!userInfo.getEncryptPassword().equals(password)) {
            log.error("密码不一致");
            return false;
        }
        userInfo.setTelephone(currentTelephone);
        UserInfo save = userInfoRepository.save(userInfo);
        if (save != null) {
            return true;
        }
        return false;
    }

    @Override
    public UserInfo findByTelephone(String telephone) {
        UserInfo userInfo = userInfoRepository.findByTelephone(telephone);
        return userInfo;
    }

    @Override
    public boolean modifyProfilePicture(String telephone, Integer pictureId) {
        UserInfo userInfo = userInfoRepository.findByTelephone(telephone);
        userInfo.setPictureId(pictureId);
        UserInfo save = userInfoRepository.save(userInfo);
        if (save != null) {
            return true;
        }
        return false;
    }
}
